<?php
/*
 * Created on 23.04.2008
 *
 * To change the template for this generated file go to
 * Window - Preferences - PHPeclipse - PHP - Code Templates
 */
 
 class Controller_Basket extends Controller_Base_Secret {
 	
 	function index($args) {
 		$template = new Template('basket.tmpl');

		$goods = new Model_Good();
		$basket = new Model_Basket();

		$sizes 		= new Model_Size();
		$colors		= new Model_Color();
		$structures = new Model_Structure();

 		$template->setArgs(
 			array( 
				_user		=> $this->user,
				menu		=> $this->menu->getMenu(1),
				
				in_basket	=> $goods->getList($basket->getIds($this->user->id)),
				
				sizes		=> $sizes->getList(), 
				structures	=> $structures->getList(),
				goods		=> $goods->find(array(category => $args[0])),
				categories	=> $goods->getCategories(),
			)
		);
		
		return array(
 			status 		=> 0,
 			template	=> $template
 		); 
 	}
 	
 	function add($args) {
 		
 		$template	= new Template('basket_add.tmpl');
 		
 		$basket = new Model_Basket();
 		
 		$goods = new Model_Good();
 		
 		$color 		= new Model_Color();
 		$size 		= new Model_Size();
 		
 		if($_GET[id]){
 			$item = $goods->find(array(id => $_GET[id]));
 		} else if ($_GET[suit]) {
 			$suit = new Model_Suit();	
			foreach($suit->find(array(id => $_GET[suit])) as $g) {

				$colors = array();
				foreach($color->getListByMask($g->size) as $c) {
					if(!$colors[0])
						$colors[0] = $c[description];
				}

				$basket->add(
					$this->user->id,						//user id 
					$g->id, 								//id
					1, 										//quantity 
					count($size->getListByMask($g->size)),	//sizes
					$colors,								//colors
					$g->price, 								//price
					1
				);
			}

	 		return array(
	 			status 		=> 302,
	 			location	=> '/catalog/suits'
	 		);			
			
 			//$item = $goods->find(array(id => $_GET[id]));
 		} else if (!isset($_POST['action'])) {
	 		return array(
	 			status 		=> 302,
	 			location	=> '/basket/'
	 		); 	 			
 		}
 		
 		
 		

 		$structures = new Model_Structure();
 		
 		$template->setArgs(array(
			_user			=> $this->user,
			menu			=> $this->menu->getMenu(1),
			good 			=> array(
	 			description => $item->description,
	 			name		=> $item->name,
	 			art			=> $item->art,
	 			size		=> $item->size,
//	 			color		=> $item->color,
//	 			structure	=> $item->structure,
	 			exists		=> $item->exists,
	 			price		=> $item->price
 			),
 			sizes			=> $size->getList(),
 			sizes_quantity	=> count($size->getListByMask($item->size)),
 			colors			=> $color->getListByMask($item->color),
 			structures		=> $structures->getList(),
 			id				=> $_GET[id],
 		));
 		
 		if($_POST['action'] === 'add') {
			
	 		$basket->add($this->user->id, $_POST[id], $_POST[quantity], $_POST[sizes] , $_POST[colors], $goods->find(array(id => $_POST[id]))->price );
	 		
	 		return array(
	 			status 		=> 302,
	 			location	=> '/catalog/'
	 		); 			
 		}
 		
 		return array(
 			status 		=> 0,
 			template	=> $template
 		);


 	}
 	
 	function delete($args) {
 		$basket = new Model_Basket();
 		
 		if($_GET[id])
 			$basket->delete($this->user->id,$_GET[id]);
 		else
 			$basket->clear($this->user->id);
 		
 		return array(
 			status 		=> 302,
 			location	=> '/basket/'
 		); 		
 		
 	}
 	
 	
 }
 
?>
